<template>
  <Dialog v-model="dialogVisible" title="APIJSON权限" width="90%">
    <el-form ref="formRef" v-loading="formLoading" :model="formData" label-width="80px">
      <el-form-item label="角色名称">
        <el-tag>{{ formData.name }}</el-tag>
      </el-form-item>
      <el-form-item label="角色标识">
        <el-tag>{{ formData.code }}</el-tag>
      </el-form-item>
      <el-form-item label="APIJSON权限">
        <el-card class="w-full h-400px !overflow-y-scroll" shadow="never">
          <el-table
            :data="accessData"
            style="width: 100%"
            @select="selectableCol"
            @select-all="selectableAll"
          >
            <el-table-column type="selection" width="55" />
            <el-table-column prop="alias" label="业务名" width="180" />
            <el-table-column prop="get" label="get" width="180">
              <template #default="scope">
                <el-switch v-model="scope.row.get" />
              </template>
            </el-table-column>
            <el-table-column prop="post" label="post" width="180">
              <template #default="scope">
                <el-switch v-model="scope.row.post" />
              </template>
            </el-table-column>
            <el-table-column prop="put" label="put" width="180">
              <template #default="scope">
                <el-switch v-model="scope.row.put" />
              </template>
            </el-table-column>
            <el-table-column prop="delete" label="delete" width="180">
              <template #default="scope">
                <el-switch v-model="scope.row.delete" />
              </template>
            </el-table-column>
            <el-table-column prop="gets" label="gets" width="180">
              <template #default="scope">
                <el-switch v-model="scope.row.gets" />
              </template>
            </el-table-column>
            <el-table-column prop="heads" label="heads" width="180">
              <template #default="scope">
                <el-switch v-model="scope.row.heads" />
              </template>
            </el-table-column>
            <el-table-column prop="crud" label="crud" width="180">
              <template #default="scope">
                <el-switch v-model="scope.row.crud" />
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import * as RoleApi from '@/api/system/role'
import { AccessAllDataRespVO, AccessApi } from '@/api/apijson/access'
import * as RoleAccessApi from '@/api/system/roleaccess'

defineOptions({ name: 'ApiJsonForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = reactive({
  id: 0,
  name: '',
  code: '',
  menuIds: []
})
const accessData = ref<AccessAllDataRespVO[]>([])
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (row: RoleApi.RoleVO) => {
  dialogVisible.value = true
  resetForm()
  // 设置数据
  formData.id = row.id
  formData.name = row.name
  formData.code = row.code
  formLoading.value = true
  try {
    //加载列表
    accessData.value = await AccessApi.getAllAccess({ roleId: row.id })
  } finally {
    formLoading.value = false
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    //提交数据
    await RoleAccessApi.addRoleAccess({
      roleId: formData.id,
      accessAllDataReqVOList: accessData.value
    })
    message.success(t('common.updateSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

const selectableCol = (_selection: AccessAllDataRespVO[], row: AccessAllDataRespVO) => {
  if (row.methodsBool) {
    row.methodsBool =
      row.get =
      row.head =
      row.gets =
      row.heads =
      row.post =
      row.put =
      row.delete =
      row.crud =
        false
  } else {
    row.methodsBool =
      row.get =
      row.head =
      row.gets =
      row.heads =
      row.post =
      row.put =
      row.delete =
      row.crud =
        true
  }
}
const selectableAll = (selection: AccessAllDataRespVO[]) => {
  if (selection.length == 0) {
    accessData.value.forEach((row) => {
      row.methodsBool =
        row.get =
        row.head =
        row.gets =
        row.heads =
        row.post =
        row.put =
        row.delete =
        row.crud =
          false
    })
  } else {
    selection.forEach((row) => {
      row.methodsBool =
        row.get =
        row.head =
        row.gets =
        row.heads =
        row.post =
        row.put =
        row.delete =
        row.crud =
          true
    })
  }
}
/** 重置表单 */
const resetForm = () => {
  // 重置选项
}
</script>
